java混合图片,颜色为白色
public class BlendablePicture extends Picture {
public BlendablePicture(String filename) {
super(filename);
}
public void blendRectWithWhite(int xMin, int yMin, int xMax, int yMax,
double a) {
int x;
x = xMin;
while (x <= xMax) {
int y;
y = yMin;
while (y <= yMax) {
Pixel refPix = this.getPixel(x, y);
refPix.setRed((int) Math.round(refPix.getRed() * (1.0 + a)));
refPix.setGreen((int) Math.round(refPix.getGreen() * (1.0 + a)));
refPix.setBlue((int) Math.round(refPix.getBlue() * (1.0 + a)));
y = y + 1;
}
}
}
}
我需要混合颜色的像素白色,但相反,这个代码只是使每件事更光明!它需要如下所示:
任何与此代码相关的帮助都将不胜感激
# 1 楼答案
而不是
refPix.setRed ( (int) Math.round (refPix.getRed () * (1.0+ a) ));
试试像这样的东西
refPix.setRed ( (int) Math.round (refPix.getRed()*(1.0-a)+255*a ));
当a=1.0时,得到R*0.0+255*1.0=255
当a=0.0时,得到R*1.0+255*0.0=R
当a=0.5时,得到R*0.5+255*0.5(一半)
这适用于任何颜色,不仅仅是白色,您只需将红色、绿色和蓝色的255替换为您想要混合的颜色,就可以得到RGB平均混合